package org.example.aidemo.function;


import org.example.aidemo.pojo.Order;
import org.example.aidemo.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.function.Function;

@Component
public class OrderQueryFunction implements Function<OrderQueryFunction.Request, OrderQueryFunction.Response> {
    @Autowired
    private OrderService orderService ;
    @Override
    public Response apply(Request request) {
        try {
            Order order = orderService.getOrderById(request.orderId());
            if (order != null) {
                return new Response(order);
            } else {
                return new Response(
                        request.orderId(),
                        "订单不存在",
                        "未知",
                        "未知",
                        "未知"
                );
            }
        } catch (Exception e) {
            return new Response(
                    request.orderId(),
                    "查询异常",
                    "错误",
                    "错误",
                    "错误"
            );
        }
    }
    public record Request(String orderId) {}
    public record Response(String id, String name, String username, String address, String createTime) {
        public Response(Order order) {
            this(
                    order.getId(),
                    order.getName(),
                    order.getUsername(),
                    order.getAddress(),
                    order.getCreateTime() != null ? order.getCreateTime().toString() : "未知时间"
            );
        }
    }
}